import route from '@/router'
// 当前的路由的子模块
function getModule(currentRoute) {
    let temp, path = currentRoute.path;
    let moduleName = path.split('/')[1]
    moduleName && route.options.routes.map(ele => {
        if(moduleName === ele.name) {
            temp = ele
        }
    })
    return temp
}
// 按钮权限
export default function operationConfig(currentRoute) {
    let modules = getModule(currentRoute)
    let listConfig = localStorage.getItem('menuInfoList') ? JSON.parse(localStorage.getItem('menuInfoList')) : '';
    // 权限列表的当前模块
    let matchedModule = listConfig.filter(ele => {
        return (ele.menu_url === modules.name && ele.parent_id === 200)
    });
    // 2级菜单
    let secArr = []
    listConfig.forEach(ele => {
        if(ele.parent_id === matchedModule[0].id) {
            secArr.push(ele)
        }
    })
    // 3级菜单
    let thirdArr = []
    listConfig.forEach(ele => {
        secArr.forEach(item => {
            if(item.id === ele.parent_id) {
                ele.parent_name = item.menu_name
                thirdArr.push(ele)
            }
        })
    })
    // 当前页面
    let currentPage = ''
    if(modules && modules.children) {
        modules.children.map((ele, index) => {
            if(!ele.redirect && ele.name === currentRoute.name) {
                // 没有权限的页面，提示后跳回去
                thirdArr.forEach(item => {
                    if(item.menu_name === ele.label && item.parent_name === ele.className) {
                        currentPage = item;
                    }
                })
            }
        })
    }
    // 可用的按钮
    let btnsList = []
    if(currentPage) {
        listConfig.forEach(ele => {
            if(ele.parent_id === currentPage.id) {
                btnsList.push(ele)
            }
        })
    }
    return btnsList
}